package com.snap.vseries.dialog;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.aigestudio.wheelpicker.WheelPicker;
import com.snap.vseries.R;
import com.snap.vseries.ui.widget.TextButton;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author Rachel
 */
public class CyclePickerDialog extends DialogFragment implements View.OnClickListener, WheelPicker.OnItemSelectedListener {

    TextButton confirmBtn;
    TextButton cancelBtn;
    WheelPicker wheelPicker;
    private int currentItem;
    private OnCycleSelectedListener listener;

    public DialogFragment setParam(int currentItem, OnCycleSelectedListener listener) {
        this.currentItem = currentItem;
        this.listener = listener;
        return this;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NO_TITLE, R.style.DimDialogStyle);
    }

    @Override
    public void onStart() {
        super.onStart();
        WindowManager.LayoutParams layoutParams = Objects.requireNonNull(getDialog().getWindow()).getAttributes();
        layoutParams.gravity = Gravity.BOTTOM;
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        getDialog().getWindow().setAttributes(layoutParams);
        getDialog().setCanceledOnTouchOutside(true);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.dialog_cycle_picker, container, false);
        confirmBtn = v.findViewById(R.id.confirm_btn);
        cancelBtn = v.findViewById(R.id.cancel_btn);
        wheelPicker = v.findViewById(R.id.picker);
        //选中的条目颜色
        wheelPicker.setSelectedItemTextColor(Color.BLACK);
        //未选中的条目颜色
        wheelPicker.setItemTextColor(Color.parseColor("#60000000"));
        wheelPicker.setIndicator(true);
        wheelPicker.setIndicatorColor(getContext().getResources().getColor(R.color.color_divider));
        wheelPicker.setIndicatorSize(2);
        wheelPicker.setVisibleItemCount(5);
        //是否可循环
        wheelPicker.setCyclic(true);
        //是否是弧形状态显示
        wheelPicker.setCurved(false);
        List<Integer> list = new ArrayList<>();
        for (int i = 1; i < 100; i++) {
            list.add(i);
        }
        wheelPicker.setData(list);
        wheelPicker.setSelectedItemPosition(currentItem - 1);
        confirmBtn.setOnClickListener(this);
        cancelBtn.setOnClickListener(this);
        wheelPicker.setOnItemSelectedListener(this);
        return v;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.confirm_btn:
                if (listener != null) {
                    listener.onCycleSelected(currentItem);
                }
                dismiss();
                break;
            case R.id.cancel_btn:
                dismiss();
                break;
            default:
                break;

        }
    }

    @Override
    public void onItemSelected(WheelPicker picker, Object data, int position) {
        currentItem = position + 1;
    }

    public interface OnCycleSelectedListener {
        /**
         * 循环次数改变时触发
         *
         * @param cycleCount 循环次数
         */
        void onCycleSelected(int cycleCount);
    }
}
